{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "converting layer conv1_1, wmat shape = (64, 3, 3, 3), bias shape = (64,)\n",
      "converting layer conv1_2, wmat shape = (64, 64, 3, 3), bias shape = (64,)\n",
      "converting layer conv2_1, wmat shape = (128, 64, 3, 3), bias shape = (128,)\n",
      "converting layer conv2_2, wmat shape = (128, 128, 3, 3), bias shape = (128,)\n",
      "converting layer conv3_1, wmat shape = (256, 128, 3, 3), bias shape = (256,)\n",
      "converting layer conv3_2, wmat shape = (256, 256, 3, 3), bias shape = (256,)\n",
      "converting layer conv3_3, wmat shape = (256, 256, 3, 3), bias shape = (256,)\n",
      "converting layer conv3_4, wmat shape = (256, 256, 3, 3), bias shape = (256,)\n",
      "converting layer conv4_1, wmat shape = (512, 256, 3, 3), bias shape = (512,)\n",
      "converting layer conv4_2, wmat shape = (512, 512, 3, 3), bias shape = (512,)\n",
      "converting layer conv4_3_CPM, wmat shape = (256, 512, 3, 3), bias shape = (256,)\n",
      "converting layer conv4_4_CPM, wmat shape = (128, 256, 3, 3), bias shape = (128,)\n",
      "converting layer conv5_1_CPM_L1, wmat shape = (128, 128, 3, 3), bias shape = (128,)\n",
      "converting layer conv5_1_CPM_L2, wmat shape = (128, 128, 3, 3), bias shape = (128,)\n",
      "converting layer conv5_2_CPM_L1, wmat shape = (128, 128, 3, 3), bias shape = (128,)\n",
      "converting layer conv5_2_CPM_L2, wmat shape = (128, 128, 3, 3), bias shape = (128,)\n",
      "converting layer conv5_3_CPM_L1, wmat shape = (128, 128, 3, 3), bias shape = (128,)\n",
      "converting layer conv5_3_CPM_L2, wmat shape = (128, 128, 3, 3), bias shape = (128,)\n",
      "converting layer conv5_4_CPM_L1, wmat shape = (512, 128, 1, 1), bias shape = (512,)\n",
      "converting layer conv5_4_CPM_L2, wmat shape = (512, 128, 1, 1), bias shape = (512,)\n",
      "converting layer conv5_5_CPM_L1, wmat shape = (38, 512, 1, 1), bias shape = (38,)\n",
      "converting layer conv5_5_CPM_L2, wmat shape = (19, 512, 1, 1), bias shape = (19,)\n",
      "converting layer Mconv1_stage2_L1, wmat shape = (128, 185, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv1_stage2_L2, wmat shape = (128, 185, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv2_stage2_L1, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv2_stage2_L2, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv3_stage2_L1, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv3_stage2_L2, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv4_stage2_L1, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv4_stage2_L2, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv5_stage2_L1, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv5_stage2_L2, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv6_stage2_L1, wmat shape = (128, 128, 1, 1), bias shape = (128,)\n",
      "converting layer Mconv6_stage2_L2, wmat shape = (128, 128, 1, 1), bias shape = (128,)\n",
      "converting layer Mconv7_stage2_L1, wmat shape = (38, 128, 1, 1), bias shape = (38,)\n",
      "converting layer Mconv7_stage2_L2, wmat shape = (19, 128, 1, 1), bias shape = (19,)\n",
      "converting layer Mconv1_stage3_L1, wmat shape = (128, 185, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv1_stage3_L2, wmat shape = (128, 185, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv2_stage3_L1, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv2_stage3_L2, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv3_stage3_L1, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv3_stage3_L2, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv4_stage3_L1, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv4_stage3_L2, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv5_stage3_L1, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv5_stage3_L2, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv6_stage3_L1, wmat shape = (128, 128, 1, 1), bias shape = (128,)\n",
      "converting layer Mconv6_stage3_L2, wmat shape = (128, 128, 1, 1), bias shape = (128,)\n",
      "converting layer Mconv7_stage3_L1, wmat shape = (38, 128, 1, 1), bias shape = (38,)\n",
      "converting layer Mconv7_stage3_L2, wmat shape = (19, 128, 1, 1), bias shape = (19,)\n",
      "converting layer Mconv1_stage4_L1, wmat shape = (128, 185, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv1_stage4_L2, wmat shape = (128, 185, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv2_stage4_L1, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv2_stage4_L2, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv3_stage4_L1, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv3_stage4_L2, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv4_stage4_L1, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv4_stage4_L2, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv5_stage4_L1, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv5_stage4_L2, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv6_stage4_L1, wmat shape = (128, 128, 1, 1), bias shape = (128,)\n",
      "converting layer Mconv6_stage4_L2, wmat shape = (128, 128, 1, 1), bias shape = (128,)\n",
      "converting layer Mconv7_stage4_L1, wmat shape = (38, 128, 1, 1), bias shape = (38,)\n",
      "converting layer Mconv7_stage4_L2, wmat shape = (19, 128, 1, 1), bias shape = (19,)\n",
      "converting layer Mconv1_stage5_L1, wmat shape = (128, 185, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv1_stage5_L2, wmat shape = (128, 185, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv2_stage5_L1, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv2_stage5_L2, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv3_stage5_L1, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv3_stage5_L2, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv4_stage5_L1, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv4_stage5_L2, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv5_stage5_L1, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv5_stage5_L2, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv6_stage5_L1, wmat shape = (128, 128, 1, 1), bias shape = (128,)\n",
      "converting layer Mconv6_stage5_L2, wmat shape = (128, 128, 1, 1), bias shape = (128,)\n",
      "converting layer Mconv7_stage5_L1, wmat shape = (38, 128, 1, 1), bias shape = (38,)\n",
      "converting layer Mconv7_stage5_L2, wmat shape = (19, 128, 1, 1), bias shape = (19,)\n",
      "converting layer Mconv1_stage6_L1, wmat shape = (128, 185, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv1_stage6_L2, wmat shape = (128, 185, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv2_stage6_L1, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv2_stage6_L2, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv3_stage6_L1, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv3_stage6_L2, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv4_stage6_L1, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv4_stage6_L2, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv5_stage6_L1, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv5_stage6_L2, wmat shape = (128, 128, 7, 7), bias shape = (128,)\n",
      "converting layer Mconv6_stage6_L1, wmat shape = (128, 128, 1, 1), bias shape = (128,)\n",
      "converting layer Mconv6_stage6_L2, wmat shape = (128, 128, 1, 1), bias shape = (128,)\n",
      "converting layer Mconv7_stage6_L1, wmat shape = (38, 128, 1, 1), bias shape = (38,)\n",
      "converting layer Mconv7_stage6_L2, wmat shape = (19, 128, 1, 1), bias shape = (19,)\n",
      "self._data_shapes:  [DataDesc[data,(1, 3, 368, 368),<type 'numpy.float32'>,NCHW]]\n",
      "self._label_shapes:  None\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "../../../practice_demo/mxnet/module/base_module.py:52: UserWarning: \u001b[91mYou created Module with Module(..., label_names=['prob_label']) but input with name 'prob_label' is not found in symbol.list_arguments(). Did you mean one of:\n",
      "\tdata\u001b[0m\n",
      "  warnings.warn(msg)\n",
      "../../../practice_demo/mxnet/module/base_module.py:64: UserWarning: Data provided by label_shapes don't match names specified by label_names ([] vs. ['prob_label'])\n",
      "  warnings.warn(msg)\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "import numpy as np\n",
    "sys.path.append('../../../practice_demo')\n",
    "\n",
    "sys.path.append('/data/guest_users/liangdong/liangdong/src/Realtime_Multi-Person_Pose_Estimation/caffe_demo/python')\n",
    "\n",
    "import caffe\n",
    "from caffe.proto import caffe_pb2\n",
    "use_caffe = True\n",
    "import re\n",
    "from google.protobuf import text_format\n",
    "\n",
    "model={}\n",
    "currentdir = '../model/_trained_COCO/'\n",
    "model['caffemodel'] = currentdir+'pose_iter_440000.caffemodel'\n",
    "model['deployFile'] = currentdir+'pose_deploy.prototxt'\n",
    "\n",
    "def read_prototxt(fname):\n",
    "    \"\"\"Return a caffe_pb2.NetParameter object that defined in a prototxt file\n",
    "    \"\"\"\n",
    "    proto = caffe_pb2.NetParameter()\n",
    "    with open(fname, 'r') as f:\n",
    "        text_format.Merge(str(f.read()), proto)\n",
    "    return proto\n",
    "\n",
    "def get_layers(proto):\n",
    "    \"\"\"Returns layers in a caffe_pb2.NetParameter object\n",
    "    \"\"\"\n",
    "    if len(proto.layer):\n",
    "        return proto.layer\n",
    "    elif len(proto.layers):\n",
    "        return proto.layers\n",
    "    else:\n",
    "        raise ValueError('Invalid proto file.')\n",
    "        \n",
    "myfile = read_prototxt(model['deployFile'])\n",
    "mylayers = get_layers(myfile)\n",
    "len(mylayers)\n",
    "\n",
    "def _get_input(proto):\n",
    "    \"\"\"Get input size\n",
    "    \"\"\"\n",
    "    layer = get_layers(proto)\n",
    "    if len(proto.input_dim) > 0:\n",
    "        input_dim = proto.input_dim\n",
    "    elif len(proto.input_shape) > 0:\n",
    "        input_dim = proto.input_shape[0].dim\n",
    "    elif layer[0].type == \"Input\":\n",
    "        input_dim = layer[0].input_param.shape[0].dim\n",
    "        layer.pop(0)\n",
    "    else:\n",
    "        raise ValueError('Cannot find input size')\n",
    "\n",
    "    assert layer[0].type != \"Input\", 'only support single input'\n",
    "    # We assume the first bottom blob of first layer is the output from data layer\n",
    "    input_name = layer[0].bottom[0]\n",
    "    return input_name, input_dim, layer\n",
    "\n",
    "def _convert_conv_param(param):\n",
    "    \"\"\"Convert convolution layer parameter from Caffe to MXNet\n",
    "    \"\"\"\n",
    "    pad = 0\n",
    "    if isinstance(param.pad, int):\n",
    "        pad = param.pad\n",
    "    else:\n",
    "        pad = 0 if len(param.pad) == 0 else param.pad[0]\n",
    "    stride = 1\n",
    "    if isinstance(param.stride, int):\n",
    "        stride = param.stride\n",
    "    else:\n",
    "        stride = 1 if len(param.stride) == 0 else param.stride[0]\n",
    "    kernel_size = ''\n",
    "    if isinstance(param.kernel_size, int):\n",
    "        kernel_size = param.kernel_size\n",
    "    else:\n",
    "        kernel_size = param.kernel_size[0]\n",
    "    dilate = 1\n",
    "    if isinstance(param.dilation, int):\n",
    "        dilate = param.dilation\n",
    "    else:\n",
    "        dilate = 1 if len(param.dilation) == 0 else param.dilation[0]\n",
    "    # convert to string except for dilation\n",
    "    param_string = \"num_filter=%d, pad=(%d,%d), kernel=(%d,%d), stride=(%d,%d), no_bias=%s\" % \\\n",
    "                   (param.num_output, pad, pad, kernel_size, kernel_size, stride, stride, not param.bias_term)\n",
    "    # deal with dilation. Won't be in deconvolution\n",
    "    if dilate > 1:\n",
    "        param_string += \", dilate=(%d, %d)\" % (dilate, dilate)\n",
    "    return param_string\n",
    "\n",
    "def _convert_pooling_param(param):\n",
    "    \"\"\"Convert the pooling layer parameter\n",
    "    \"\"\"\n",
    "    param_string = \"pooling_convention='full', \"\n",
    "    if param.global_pooling:\n",
    "        param_string += \"global_pool=True, kernel=(1,1)\"\n",
    "    else:\n",
    "        param_string += \"pad=(%d,%d), kernel=(%d,%d), stride=(%d,%d)\" % (\n",
    "            param.pad, param.pad, param.kernel_size, param.kernel_size,\n",
    "            param.stride, param.stride)\n",
    "    if param.pool == 0:\n",
    "        param_string += \", pool_type='max'\"\n",
    "    elif param.pool == 1:\n",
    "        param_string += \", pool_type='avg'\"\n",
    "    else:\n",
    "        raise ValueError(\"Unknown Pooling Method!\")\n",
    "    return param_string\n",
    "\n",
    "def _parse_proto(prototxt_fname):\n",
    "    \"\"\"Parse Caffe prototxt into symbol string\n",
    "    \"\"\"\n",
    "    proto = read_prototxt(prototxt_fname)\n",
    "\n",
    "    # process data layer\n",
    "    input_name, input_dim, layer = _get_input(proto)\n",
    "    # only support single input, so always use `data` as the input data\n",
    "    mapping = {input_name: 'data'}\n",
    "    need_flatten = {input_name: False}\n",
    "    symbol_string = \"import mxnet as mx\\n\" \\\n",
    "                    + \"data = mx.symbol.Variable(name='data')\\n\";\n",
    "\n",
    "    connection = dict()\n",
    "    symbols = dict()\n",
    "    top = dict()\n",
    "    flatten_count = 0\n",
    "    output_name = \"\"\n",
    "    prev_name = None\n",
    "\n",
    "    # convert reset layers one by one\n",
    "    for i in range(len(layer)):\n",
    "        type_string = ''\n",
    "        param_string = ''\n",
    "        skip_layer = False\n",
    "        \n",
    "        name = re.sub('[-/]', '_', layer[i].name)\n",
    "        if layer[i].type == 'Convolution' or layer[i].type == 4:\n",
    "            type_string = 'mx.symbol.Convolution'\n",
    "            param_string = _convert_conv_param(layer[i].convolution_param)\n",
    "            need_flatten[name] = True\n",
    "        if layer[i].type == 'Deconvolution' or layer[i].type == 39:\n",
    "            type_string = 'mx.symbol.Deconvolution'\n",
    "            param_string = _convert_conv_param(layer[i].convolution_param)\n",
    "            need_flatten[name] = True\n",
    "        if layer[i].type == 'Pooling' or layer[i].type == 17:\n",
    "            type_string = 'mx.symbol.Pooling'\n",
    "            param_string = _convert_pooling_param(layer[i].pooling_param)\n",
    "            need_flatten[name] = True\n",
    "        if layer[i].type == 'ReLU' or layer[i].type == 18:\n",
    "            type_string = 'mx.symbol.Activation'\n",
    "            param_string = \"act_type='relu'\"\n",
    "            need_flatten[name] = need_flatten[mapping[layer[i].bottom[0]]]\n",
    "        if layer[i].type == 'TanH' or layer[i].type == 23:\n",
    "            type_string = 'mx.symbol.Activation'\n",
    "            param_string = \"act_type='tanh'\"\n",
    "            need_flatten[name] = need_flatten[mapping[layer[i].bottom[0]]]\n",
    "        if layer[i].type == 'Sigmoid' or layer[i].type == 19:\n",
    "            type_string = 'mx.symbol.Activation'\n",
    "            param_string = \"act_type='sigmoid'\"\n",
    "            need_flatten[name] = need_flatten[mapping[layer[i].bottom[0]]]\n",
    "        if layer[i].type == 'LRN' or layer[i].type == 15:\n",
    "            type_string = 'mx.symbol.LRN'\n",
    "            param = layer[i].lrn_param\n",
    "            param_string = \"alpha=%f, beta=%f, knorm=%f, nsize=%d\" % (\n",
    "                param.alpha, param.beta, param.k, param.local_size)\n",
    "            need_flatten[name] = True\n",
    "        if layer[i].type == 'InnerProduct' or layer[i].type == 14:\n",
    "            type_string = 'mx.symbol.FullyConnected'\n",
    "            param = layer[i].inner_product_param\n",
    "            param_string = \"num_hidden=%d, no_bias=%s\" % (\n",
    "                param.num_output, not param.bias_term)\n",
    "            need_flatten[name] = False\n",
    "        if layer[i].type == 'Dropout' or layer[i].type == 6:\n",
    "            type_string = 'mx.symbol.Dropout'\n",
    "            param = layer[i].dropout_param\n",
    "            param_string = \"p=%f\" % param.dropout_ratio\n",
    "            need_flatten[name] = need_flatten[mapping[layer[i].bottom[0]]]\n",
    "        if layer[i].type == 'Softmax' or layer[i].type == 20:\n",
    "            type_string = 'mx.symbol.SoftmaxOutput'\n",
    "        if layer[i].type == 'Flatten' or layer[i].type == 8:\n",
    "            type_string = 'mx.symbol.Flatten'\n",
    "            need_flatten[name] = False\n",
    "        if layer[i].type == 'Split' or layer[i].type == 22:\n",
    "            type_string = 'split'  # will process later\n",
    "        if layer[i].type == 'Concat' or layer[i].type == 3:\n",
    "            type_string = 'mx.symbol.Concat'\n",
    "            need_flatten[name] = True\n",
    "        if layer[i].type == 'Crop':\n",
    "            type_string = 'mx.symbol.Crop'\n",
    "            need_flatten[name] = True\n",
    "            param_string = 'center_crop=True'\n",
    "        if layer[i].type == 'BatchNorm':\n",
    "            type_string = 'mx.symbol.BatchNorm'\n",
    "            param = layer[i].batch_norm_param\n",
    "            param_string = 'use_global_stats=%s, fix_gamma=False' % param.use_global_stats\n",
    "            need_flatten[name] = need_flatten[mapping[layer[i].bottom[0]]]\n",
    "        if layer[i].type == 'Scale':\n",
    "            assert layer[i-1].type == 'BatchNorm'\n",
    "            need_flatten[name] = need_flatten[mapping[layer[i].bottom[0]]]\n",
    "            skip_layer = True\n",
    "            prev_name = re.sub('[-/]', '_', layer[i-1].name)\n",
    "        if layer[i].type == 'PReLU':\n",
    "            type_string = 'mx.symbol.LeakyReLU'\n",
    "            param = layer[i].prelu_param\n",
    "            param_string = \"act_type='prelu', slope=%f\" % param.filler.value\n",
    "            need_flatten[name] = need_flatten[mapping[layer[i].bottom[0]]]\n",
    "        if layer[i].type == 'Eltwise':\n",
    "            type_string = 'mx.symbol.broadcast_add'\n",
    "            param_string = \"\"\n",
    "            need_flatten[name] = False\n",
    "        if layer[i].type == 'Reshape':\n",
    "            type_string = 'mx.symbol.Reshape'\n",
    "            need_flatten[name] = False\n",
    "            param = layer[i].reshape_param\n",
    "            param_string = \"shape=(%s)\" % (','.join(param.shape.dim),)\n",
    "\n",
    "        if skip_layer:\n",
    "            assert len(layer[i].bottom) == 1\n",
    "            symbol_string += \"%s = %s\\n\" % (name, prev_name)\n",
    "        elif type_string == '':\n",
    "            raise ValueError('Unknown layer %s!' % layer[i].type)\n",
    "        elif type_string != 'split':\n",
    "            bottom = layer[i].bottom\n",
    "            if param_string != \"\":\n",
    "                param_string = \", \" + param_string\n",
    "            if len(bottom) == 1:\n",
    "                if need_flatten[mapping[bottom[0]]] and type_string == 'mx.symbol.FullyConnected':\n",
    "                    flatten_name = \"flatten_%d\" % flatten_count\n",
    "                    symbol_string += \"%s=mx.symbol.Flatten(name='%s', data=%s)\\n\" % (\n",
    "                        flatten_name, flatten_name, mapping[bottom[0]])\n",
    "                    flatten_count += 1\n",
    "                    need_flatten[flatten_name] = False\n",
    "                    bottom[0] = flatten_name\n",
    "                    mapping[bottom[0]] = bottom[0]\n",
    "                symbol_string += \"%s = %s(name='%s', data=%s %s)\\n\" % (\n",
    "                    name, type_string, name, mapping[bottom[0]], param_string)\n",
    "            else:\n",
    "                symbol_string += \"%s = %s(name='%s', *[%s] %s)\\n\" % (\n",
    "                    name, type_string, name, ','.join([mapping[x] for x in bottom]), param_string)\n",
    "        for j in range(len(layer[i].top)):\n",
    "            mapping[layer[i].top[j]] = name\n",
    "        output_name = name\n",
    "    return symbol_string, output_name, input_dim\n",
    "\n",
    "symbol_string, output_name, input_dim = _parse_proto(model['deployFile'])\n",
    "\n",
    "exec(symbol_string)\n",
    "\n",
    "heatmaplabel = mx.sym.Variable(\"heatmaplabel\")\n",
    "partaffinityglabel = mx.sym.Variable('partaffinityglabel')\n",
    "\n",
    "pag_loss_layer1 = mx.sym.LinearRegressionOutput(data=Mconv7_stage6_L2, label=heatmaplabel)\n",
    "confid_loss_layer1 = mx.sym.LinearRegressionOutput(data=Mconv7_stage6_L1, label=partaffinityglabel)\n",
    "group = mx.symbol.Group([Mconv7_stage6_L1,Mconv7_stage6_L2])\n",
    "sym  = group\n",
    "\n",
    "\n",
    "arg_shapes, output_shapes, aux_shapes = sym.infer_shape(data=tuple(input_dim))\n",
    "\n",
    "\n",
    "\n",
    "arg_names = sym.list_arguments()\n",
    "aux_names = sym.list_auxiliary_states()\n",
    "arg_shape_dic = dict(zip(arg_names, arg_shapes))\n",
    "aux_shape_dic = dict(zip(aux_names, aux_shapes))\n",
    "arg_params = {}\n",
    "aux_params = {}\n",
    "first_conv = True\n",
    "\n",
    "def read_caffemodel(prototxt_fname, caffemodel_fname):\n",
    "    \"\"\"Return a caffe_pb2.NetParameter object that defined in a binary\n",
    "    caffemodel file\n",
    "    \"\"\"\n",
    "    if use_caffe:\n",
    "        caffe.set_mode_cpu()\n",
    "        net = caffe.Net(prototxt_fname, caffemodel_fname, caffe.TEST)\n",
    "        layer_names = net._layer_names\n",
    "        layers = net.layers\n",
    "        return (layers, layer_names)\n",
    "    else:\n",
    "        proto = caffe_pb2.NetParameter()\n",
    "        with open(caffemodel_fname, 'rb') as f:\n",
    "            proto.ParseFromString(f.read())\n",
    "        return (get_layers(proto), None)\n",
    "\n",
    "layers, names = read_caffemodel(model['deployFile'], model['caffemodel'])\n",
    "\n",
    "def clayer_iter(layers, layer_names):\n",
    "    if use_caffe:\n",
    "        for layer_idx, layer in enumerate(layers):\n",
    "            layer_name = re.sub('[-/]', '_', layer_names[layer_idx])\n",
    "            layer_type = layer.type\n",
    "            layer_blobs = layer.blobs\n",
    "            yield (layer_name, layer_type, layer_blobs)\n",
    "    else:\n",
    "        for layer in layers:\n",
    "            layer_name = re.sub('[-/]', '_', layer.name)\n",
    "            layer_type = layer.type\n",
    "            layer_blobs = layer.blobs\n",
    "            yield (layer_name, layer_type, layer_blobs)\n",
    "            \n",
    "layer_iter = clayer_iter(layers, names)\n",
    "layer_names=[]\n",
    "layer_types=[]\n",
    "\n",
    "for layer_name, layer_type, layer_blobs in layer_iter:\n",
    "    layer_names.append(layer_name)\n",
    "    layer_types.append(layer_type)\n",
    "\n",
    "mxnetlist = sym.list_arguments()\n",
    "\n",
    "\n",
    "layer_iter = clayer_iter(layers, names)\n",
    "\n",
    "for layer_name, layer_type, layer_blobs in layer_iter:\n",
    "     if layer_type == 'Convolution' or layer_type == 'InnerProduct' or layer_type == 4 or layer_type == 14 \\\n",
    "                or layer_type == 'PReLU':\n",
    "            if layer_type == 'PReLU':\n",
    "                assert (len(layer_blobs) == 1)\n",
    "                wmat = layer_blobs[0].data\n",
    "                weight_name = layer_name + '_gamma'\n",
    "                arg_params[weight_name] = mx.nd.zeros(wmat.shape)\n",
    "                arg_params[weight_name][:] = wmat\n",
    "                continue\n",
    "            wmat_dim = []\n",
    "            if getattr(layer_blobs[0].shape, 'dim', None) is not None:\n",
    "                if len(layer_blobs[0].shape.dim) > 0:\n",
    "                    wmat_dim = layer_blobs[0].shape.dim\n",
    "                else:\n",
    "                    wmat_dim = [layer_blobs[0].num, layer_blobs[0].channels, layer_blobs[0].height, layer_blobs[0].width]\n",
    "            else:\n",
    "                wmat_dim = list(layer_blobs[0].shape)\n",
    "            wmat = np.array(layer_blobs[0].data).reshape(wmat_dim)\n",
    "\n",
    "            channels = wmat_dim[1]\n",
    "            if channels == 3 or channels == 4:  # RGB or RGBA\n",
    "                if first_conv:\n",
    "                    # Swapping BGR of caffe into RGB in mxnet\n",
    "                    wmat[:, [0, 2], :, :] = wmat[:, [2, 0], :, :]\n",
    "\n",
    "            assert(wmat.flags['C_CONTIGUOUS'] is True)\n",
    "            sys.stdout.write('converting layer {0}, wmat shape = {1}'.format(layer_name, wmat.shape))\n",
    "            if len(layer_blobs) == 2:\n",
    "                bias = np.array(layer_blobs[1].data)\n",
    "                bias = bias.reshape((bias.shape[0], 1))\n",
    "                assert(bias.flags['C_CONTIGUOUS'] is True)\n",
    "                bias_name = layer_name + \"_bias\"\n",
    "                bias = bias.reshape(arg_shape_dic[bias_name])\n",
    "                arg_params[bias_name] = mx.nd.zeros(bias.shape)\n",
    "                arg_params[bias_name][:] = bias\n",
    "                sys.stdout.write(', bias shape = {}'.format(bias.shape))\n",
    "\n",
    "            sys.stdout.write('\\n')\n",
    "            sys.stdout.flush()\n",
    "            wmat = wmat.reshape((wmat.shape[0], -1))\n",
    "            weight_name = layer_name + \"_weight\"\n",
    "\n",
    "            if weight_name not in arg_shape_dic:\n",
    "                print(weight_name + ' not found in arg_shape_dic.')\n",
    "                continue\n",
    "            wmat = wmat.reshape(arg_shape_dic[weight_name])\n",
    "            arg_params[weight_name] = mx.nd.zeros(wmat.shape)\n",
    "            arg_params[weight_name][:] = wmat\n",
    "\n",
    "\n",
    "            if first_conv and (layer_type == 'Convolution' or layer_type == 4):\n",
    "                first_conv = False\n",
    "                \n",
    "\n",
    "output_prefix='realtimePose'\n",
    "cmodel = mx.mod.Module(symbol=sym, label_names=['prob_label', ])\n",
    "cmodel.bind(data_shapes=[('data', tuple(input_dim))])\n",
    "cmodel.init_params(arg_params=arg_params, aux_params=aux_params)\n",
    "cmodel.save_checkpoint(currentdir+output_prefix, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
